package com.xcity.pomelo.response;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.xcity.game.activity.cumulativerecharge.CumulativeRechargeActivityService;
import com.xcity.game.activity.cumulativerecharge.CumulativeRechargeTemplate;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class CumulativeListResponse extends Response {
	
	private int type;
	private int rechargedMoney;
	private int canRecharge;//0:不能充值了(活动结束 只能领奖充值没用了) 1:还可以充值
	private List<RechargeItem> list;
	private String startDayStr;
	private String rechargeEndStr;
	private String endDayStr;
	
	public static class CumulativeRewardItem{
		public int itemId;
		public int itemCount;
		public CumulativeRewardItem(int id, int count){
			this.itemId = id;
			this.itemCount = count;
		}
	}
	
	public static class RechargeItem{
		public int id;
		public int need;
		public int state;//0:未完成 1:完成未领 2:已领
		public int rewardMoney;
//		public int[] rewardItemIds;
//		public int[] rewardItemCounts;
		public List<CumulativeRewardItem> itemList;
		public RechargeItem(CumulativeRechargeTemplate temp, int state){
			this.id = temp.getId();
			this.need = temp.getNeed();
			this.rewardMoney = temp.getMoney();
//			this.rewardItemIds = temp.getItemIds();
//			this.rewardItemCounts = temp.getItemCounts();
			this.state = state;
			this.itemList = new ArrayList<CumulativeRewardItem>();
			for(int i = 0; i < temp.getItemIds().length; i++){
				CumulativeRewardItem item = new CumulativeRewardItem(temp.getItemIds()[i], temp.getItemCounts()[i]);
				itemList.add(item);
			}
		}
	}
	
	public CumulativeListResponse(int serial, int type, List<CumulativeRechargeTemplate> tempList, JSONObject map, int money, boolean isEnd, int startDay) {
		super(serial);
		this.type = type;
		this.rechargedMoney = money;
		this.canRecharge = isEnd? 0:1;
		list = new ArrayList<RechargeItem>();
		for(int i = tempList.size() - 1; i > -1; i--){
			CumulativeRechargeTemplate temp = tempList.get(i);
			if(temp.getType() == type){
				int state = map.getIntValue(String.valueOf(temp.getId()));//0:未领 1:已领
				if(state == 0){
					if(rechargedMoney >= temp.getNeed()){
						state = 1;//0:未完成 1:完成未领 2:已领
					}
				}else{
					state = 2;//0:未完成 1:完成未领 2:已领
				}
				RechargeItem item = new RechargeItem(temp, state);
				list.add(item);
			}
		}
		list.sort(new Comparator<RechargeItem>() {
			@Override
			public int compare(RechargeItem o1, RechargeItem o2) {
				if(o1.state == o2.state){
					return o1.id - o2.id;
				}else if(o1.state == 1){
					return -1;
				}else if(o2.state == 1){
					return 1;
				}
				return o1.state - o2.state;
			}
		});
		//时间下发
		Calendar cal = Calendar.getInstance();
		int year, moon, moonday;
		cal.set(Calendar.YEAR, startDay >> 16);
		cal.set(Calendar.DAY_OF_YEAR, startDay & 0xFFFF);
		year = cal.get(Calendar.YEAR);
		moon = cal.get(Calendar.MONTH) + 1;
		moonday = cal.get(Calendar.DAY_OF_MONTH);
		this.startDayStr = MessageFormat.format("{0}年{1}月{2}日0时", String.valueOf(year), moon, moonday);
		int activeDay = CumulativeRechargeActivityService.ACTIVE_DAY;
		int rewardDay = CumulativeRechargeActivityService.REWARD_DAY;
		if(type == 1){
			//newrole
			activeDay = CumulativeRechargeActivityService.ACTIVE_DAY_NEWROLE;
			rewardDay = CumulativeRechargeActivityService.REWARD_DAY_NEWROLE;
		}
		cal.add(Calendar.DAY_OF_MONTH, activeDay - 1);
		year = cal.get(Calendar.YEAR);
		moon = cal.get(Calendar.MONTH) + 1;
		moonday = cal.get(Calendar.DAY_OF_MONTH);
		this.rechargeEndStr = MessageFormat.format("{0}年{1}月{2}日24时", String.valueOf(year), moon, moonday);
		cal.add(Calendar.DAY_OF_MONTH, rewardDay - activeDay);
		year = cal.get(Calendar.YEAR);
		moon = cal.get(Calendar.MONTH) + 1;
		moonday = cal.get(Calendar.DAY_OF_MONTH);
		this.endDayStr = MessageFormat.format("{0}年{1}月{2}日24时", String.valueOf(year), moon, moonday);
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getRechargedMoney() {
		return rechargedMoney;
	}

	public void setRechargedMoney(int rechargedMoney) {
		this.rechargedMoney = rechargedMoney;
	}

	public int getCanRecharge() {
		return canRecharge;
	}

	public void setCanRecharge(int canRecharge) {
		this.canRecharge = canRecharge;
	}

	public List<RechargeItem> getList() {
		return list;
	}

	public void setList(List<RechargeItem> list) {
		this.list = list;
	}

	public String getStartDayStr() {
		return startDayStr;
	}

	public void setStartDayStr(String startDayStr) {
		this.startDayStr = startDayStr;
	}

	public String getRechargeEndStr() {
		return rechargeEndStr;
	}

	public void setRechargeEndStr(String rechargeEndStr) {
		this.rechargeEndStr = rechargeEndStr;
	}

	public String getEndDayStr() {
		return endDayStr;
	}

	public void setEndDayStr(String endDayStr) {
		this.endDayStr = endDayStr;
	}

	
}
